package cn.smartorigin.soas.frame.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.frame.AddClient;
import cn.smartorigin.soas.frame.AddPlateTypeFrame;
import cn.smartorigin.soas.frame.ColorTableIF;
import cn.smartorigin.soas.frame.ImageButton;
import cn.smartorigin.soas.frame.JColorTable;
import cn.smartorigin.soas.frame.MainFrame;
import cn.smartorigin.soas.frame.ModifyClient;
import cn.smartorigin.soas.frame.ModifyClientProduct;

public class ManageClientPage extends CellPagePanel implements PortletPanel, ColorTableIF{

	
	JColorTable   cTable;
	JScrollPane mClistPanel;
	
    ImageIcon minimizeIcon = new ImageIcon("res/images/add_client.png");
    ImageIcon minimizeIcon_p = new ImageIcon("res/images/add_client_p.png");
    ImageIcon printIcon = new ImageIcon("res/images/print.png");
    ImageIcon printIcon_p = new ImageIcon("res/images/print_p.png");
	
    MainFrame mf;
    
	public  ManageClientPage(MainFrame mfc){
		this.mf=mfc;
		this.setLayout(new BorderLayout());
		addClientTable();
	}
	
	private DefaultTableModel loadClientDate(){
		DefaultTableModel model = new DefaultTableModel();
	    model.addColumn("序号");
	    model.addColumn("客户名称");
	    try {
			TestData.getClientList(new SqliteConnecter(), model);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	   return model;
	   
	}
	
	private void addClientTable(){
		DefaultTableModel model = loadClientDate();
		cTable = new JColorTable(model);
		mClistPanel = new JScrollPane(cTable);
		this.add(mClistPanel, BorderLayout.CENTER);
		JPanel pl = new JPanel();
		//pl.setBackground(Color.white);
		pl.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
        ImageButton addClient = new ImageButton(minimizeIcon, minimizeIcon, minimizeIcon_p);
        addClient.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				new AddClient(ManageClientPage.this, mf);
			}
        	
        });
		pl.add(addClient);
        ImageButton printBtn= new ImageButton(printIcon, printIcon, printIcon_p);
		pl.add(printBtn);

		this.add(pl,BorderLayout.SOUTH);
	}
	
	public void flashClientTable(){
		DefaultTableModel model = loadClientDate();
		cTable.setModel(model);
		
	}
	
	@Override
	public void fullScreen(Portlet portlet) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void TableModify(int rowId) {
		// TODO Auto-generated method stub
		String clientName =  (String)cTable.getValueAt(rowId, 1);
		new  ModifyClient(clientName);
	}

	@Override
	public void TableDelete(int rowId) {
		// TODO Auto-generated method stub
		String clientName =  (String)cTable.getValueAt(rowId, 1);
		try {
			TestData.deleteClient(new SqliteConnecter(), clientName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.flashClientTable();
		this.repaint();
	}

}
